package com.touchwp.java.wechat.message;

import java.io.Serializable;
import java.util.regex.Pattern;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.touchwp.java.wechat.converter.CDATAAdapter;

@XmlAccessorType(XmlAccessType.FIELD)
public class Message implements Serializable {
	private static final long serialVersionUID = 382736532065872769L;

	public static Pattern MSG_TYPE_PATTERN = Pattern.compile("<MsgType><!\\[CDATA\\[(.*?)\\]\\]></MsgType>");
	public static final String TYPE_TEXT = "text";
	public static final String TYPE_LOCATION = "location";
	public static final String TYPE_IMAGE = "image";
	public static final String TYPE_NEWS = "news";
	public static final String TYPE_LINK = "link";
	public static final String TYPE_EVENT = "event";
	public static final String TYPE_MUSIC = "music";

	@XmlJavaTypeAdapter(value = CDATAAdapter.class)
	@XmlElement(name = "ToUserName")
	private String toUserName;

	@XmlElement(name = "FromUserName")
	@XmlJavaTypeAdapter(value = CDATAAdapter.class)
	private String fromUserName;

	@XmlElement(name = "CreateTime")
	private String createTime;

	@XmlJavaTypeAdapter(value = CDATAAdapter.class)
	@XmlElement(name = "MsgType")
	private String msgType;

	@XmlElement(name = "FuncFlag")
	private String funcFlag;

	@XmlElement(name = "MsgId")
	private String msgId;

	public String getToUserName() {
		return this.toUserName;
	}

	public void setToUserName(String toUserName) {
		this.toUserName = toUserName;
	}

	public String getFromUserName() {
		return this.fromUserName;
	}

	public void setFromUserName(String fromUserName) {
		this.fromUserName = fromUserName;
	}

	public String getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getMsgType() {
		return this.msgType;
	}

	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}

	public String getFuncFlag() {
		return this.funcFlag;
	}

	public void setFuncFlag(String funcFlag) {
		this.funcFlag = funcFlag;
	}

	public void copyTo(Message from) {
		setFromUserName(from.getToUserName());
		setToUserName(from.getFromUserName());
	}

	public String getMsgId() {
		return msgId;
	}

	public void setMsgId(String msgId) {
		this.msgId = msgId;
	}

	@Override
	public String toString() {
		return String.format("Message [toUserName=%s, fromUserName=%s, createTime=%s, msgType=%s, funcFlag=%s, msgId=%s]", toUserName, fromUserName, createTime, msgType, funcFlag, msgId);
	}
}
